#include <iostream>
#include <fstream>
#include <fcntl.h>
#include <deque>
#include "util.h"

struct RecordFile {
  RecordFile(string filename) {
    filename_ = filename;
    fd_ = open(filename.c_str(), O_RDWR | O_CREAT | O_APPEND, 0644);
    if (fd_ == -1) {
      cout << "ERROR - Can't open file!!!!" << endl;
    }
    next_record_start_ = 0;
    lock_.l_whence = SEEK_SET;
    lock_.l_start = 0;
    lock_.l_len = 0;
    lock_state_ = F_UNLCK;
  }
  deque<string> records_read_;
  uint64 next_record_start_;
  

  void WriteRecord(const string & s);

  // read from next_record_start_ to the end of the file and push the full 
  // results onto records_read_
  void Read();
  string PopRecord(); // pops the next available record
  uint64 RecordsAvailable() { return records_read_.size(); }
  void WriteLock();
  void ReadLock();
  void Unlock();


  int Pos() { return lseek(fd_, 0, SEEK_CUR); }
  string filename_;
  int fd_;
  int lock_state_;
  struct flock lock_;
  
};

int records_main(int argv, char ** argc);
